<?php
/**
 * FlickrPhotosetsPhoto class file
 * @package FlickrPhotosets
 */

/**
 * This class is responsible for the photo pages.
 * 
 * It is a regular canvas page and extends FlickrPhotosetsCanvas.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 8.5.14
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */
class FlickrPhotosetsPhoto extends FlickrPhotosetsCanvas {
	
	/**
	 * All sets & groups the photo belongs to.
	 * @var array
	 */
	public $all_contexts;
	
	/**
	 * The context of the photo.
	 * @var array
	 */
	public $context;
	
	/**
	 * The number of the current photo in the sequence.
	 * @var int
	 */
	public $current_photo;
	
	/**
	 * The array returned from flickr.photos.getExif.
	 * @var array
	 */
	public $exif_info;
	
	/**
	 * The people who have favorited this photo.
	 * @var array
	 */
	public $favorite;
	
	/**
	 * The photo's geo info.
	 * @var array
	 */
	public $geo;
	
	/**
	 * The photo's geo permissions.
	 * @var array
	 */
	public $geo_perms;
	
	/**
	 * Displaying the photo in a photoset.
	 * @var boolean
	 */
	public $in_photoset = false;
	
	/**
	 * Displaying the photo in the photostream.
	 * @var boolean
	 */
	public $in_photostream = false;
	
	/**
	 * The facebook id of the tagged user.
	 * @var int
	 */
	public $of;
	
	/**
	 * Displaying the photo of a user.
	 * @var boolean
	 */
	public $of_user = false;
	
	/**
	 * The photo's info.
	 * @var array
	 */
	public $photo;
	
	/**
	 * The total number of photos in the sequence.
	 * @var int
	 */
	public $photo_count;
	
	/**
	 * The array of other photos in the set/stream.
	 * @var array
	 */
	public $photos;
	
	/**
	 * The photoset's info.
	 * @var array
	 */
	public $photoset;
	
	/**
	 * Whether or not to show geodata.
	 * @var boolean
	 */
	public $show_geodata = false;
	
	/**
	 * The available sizes of the photo
	 * @var array
	 */
	public $sizes;
	
	/**
	 * The tab to set as selected.
	 * @var string
	 */
	public $tab;
	
	/**
	 * The types of tags.
	 * @var array
	 */
	public $tag_types = array(
		'user' => 'user',
		'regular' => 'regular'
	);
	
	/**
	 * The title to use for the page.
	 * @var string
	 */
	public $title;
	
	/**
	 * String to append at the end of next/prev links.
	 * @var string
	 */
	public $url_append;
	
	/**
	 * Retrieves all of the information about the photo.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct($pid = '', $get_all_info = true) {
		parent::__construct();
		
		$this->facebook->set_user($this->uid, $this->config['facebook_session_key']);
		
		// Require the photo id
		if (empty($pid)) {
			$this->require_variables($_GET['pid']);
			$pid = $_GET['pid'];
		}
		
		// Get information on the photo
		//$this->photo = $this->flickr->photos_getInfo($_GET['pid']);
		$this->flickr->request("flickr.photos.getInfo", array("photo_id"=>$pid, "secret"=>null), true);
		$this->photo = $this->flickr->parsed_response ? $this->flickr->parsed_response['photo'] : false;
		
		if ($get_all_info) {
			
			// Get the user's info from Flickr
			$this->flickr_user = $this->flickr->people_getInfo($this->nsid);
			
			// Get all of the photo's info
			$this->all_contexts = $this->flickr->photos_getAllContexts($this->photo['id']);
			$this->exif_info = $this->flickr->photos_getExif($this->photo['id']);
			$this->favorite = $this->flickr->photos_getFavorites($this->photo['id']);
			$this->geo = $this->photo['location']; //$this->flickr->photos_geo_getLocation($this->photo['id']);
			$this->geo_perms = $this->photo['geoperms'];
			$this->sizes = $this->flickr->photos_getSizes($this->photo['id']);
			
		}
		
		// Get info based on where the photo is
		if (isset($_GET['sid'])) {
			$this->in_photoset = true;
			$this->in_photoset();
		}
		else if (isset($_GET['of'])) {
			$this->of_user = true;
			$this->of_user();
		}
		else {
			$this->in_photostream = true;
			$this->in_photostream();
		}
		
		// Make sure the next and prev photos are correct
		$this->check_contexts();
		
		// Determine whether we can show geodata
		$this->show_geodata = $this->check_geo_perms();
		
		// Figure out the page that the photo appears on in the index
		$this->determine_index_page();
		
	}
	
	/**
	 * Ensures that the previous and next photos are correct.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function check_contexts() {
		// If there is no previous photo, go to the last one
		if ($this->context['prevphoto']['id'] == 0) {
			$this->context['prevphoto']['id'] = $this->photos['photo'][$this->photo_count - 1]['id'];
		}
		
		// If there is no next photo, go to the first photo
		if ($this->context['nextphoto']['id'] == 0) {
			$this->context['nextphoto']['id'] = $this->photos['photo'][0]['id'];
		}
	}
	
	/**
	 * Returns true or false depending on whether we can show geodata.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return boolean True or false depending on whether we can show geodata
	 */
	private function check_geo_perms() {
		
		// Geo permissions are public
		if ($this->geo_perms['ispublic']) {
			return true;
		}
		// Geo permissions are for friends and family
		if ($this->geo_perms['isfriend'] and $this->geo_perms['isfamily']) {
			if ($this->config['privacy_filter'] == $this->privacy_filter['friends_family']) {
				return true;
			}
		}
		// Geo permissions are for frients
		if ($this->geo_perms['isfriend']) {
			if ($this->config['privacy_filter'] == $this->privacy_filter['friends']) {
				return true;
			}
		}
		// Geo permissions are for family
		if ($this->geo_perms['isfamily']) {
			if ($this->config['privacy_filter'] == $this->privacy_filter['family']) {
				return true;
			}
		}
		// Geo permissions are private
		if ($this->config['privacy_filter'] == $this->privacy_filter['private']) {
			return true;
		}
		return false;
		
	}
	
	/**
	 * Figure out which page the photo is on in the index.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function determine_index_page() {
		if ($this->in_photoset or $this->in_photostream) {
			$this->page = ceil($this->current_photo / $this->photos_per_page);
		}
		else if ($this->of_user) {
			// Will add proper paging support later...
		}
	}
	
	/**
	 * Gets the list of the other places the photo appears.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return string The list of contexts
	 */
	public function get_all_contexts() {
		if (isset($this->all_contexts['set']) or isset($this->all_contexts['pool'])) {
			
			// The sets the photo appears in
			if (isset($this->all_contexts['set'])) {
				foreach ($this->all_contexts['set'] as $set) {
					if ($this->photoset['id'] != $set['id']) {
						$contexts .= '<li><a href="'.$this->canvas_url.'/photos/'.$this->uid.'/'.$this->photo['id'].'/in/set-'.$set['id'].'/">'.$set['title'].'</a> <a href="'.$this->canvas_url.'/photos/'.$this->uid.'/sets/'.$set['id'].'/">(Set)</a></li>';
					}
				}
			}
			
			// The pools the photo appears in
			if (isset($this->all_contexts['pool'])) {
				foreach ($this->all_contexts['pool'] as $pool) {
					$contexts .= '<li>'.$pool['title'].' (Pool)</li>';
				}
			}
			
			// If we have other contexts, wrap them in the div and list
			if (!empty($contexts)) {
				$contexts = '<div id="other_contexts">Also in: <ul>'.$contexts.'</ul></div>';
			}
			
		}
		return $contexts;
	}
	
	/**
	 * Gets a friendly string for the camera make and model.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param array $exif_info The array returned from flickr.photos.getExif
	 * @param boolean $append_a Append 'a' (or 'an') to the beginning
	 * @return string The friendly string
	 */
	public static function get_camera_string($exif_info, $append_a = false) {
		
		// Search for the make and model info
		$make_key = null;
		$model_key = null;
		foreach ($exif_info['exif'] as $key => $exif) {
			
			// Make
			$search = array_search('Make', $exif, true);
			if ($search !== false) {
				$make_key = $key;
			}
			
			// Model
			$search = array_search('Model', $exif, true);
			if ($search !== false) {
				$model_key = $key;
			}
			
		}
		
		// Get the raw data
		$make_raw = $exif_info['exif'][$make_key]['raw'];
		$model_raw = $exif_info['exif'][$model_key]['raw'];
		
		// Format the make a little
		$make = substr($make_raw, 0, strpos($make_raw, ' '));
		
		// See if the model begins with the make and if it does, remove it
		$temp = substr($model_raw, 0, strpos($model_raw, ' '));
		$model = ($temp === $make) ? substr($model_raw, strpos($model_raw, ' ')) : $model_raw;
		
		// Assemble the friendly string
		if (empty($make) and empty($model)) {
			$friendly = null;
			$append_a = false;
		}
		else {
			$friendly = ucwords(strtolower($make)).' '.$model;
		}
		
		// Append if requested
		if ($append_a) {
			
			// List of known manufacturers that don't work below with proper a/an
			$mfgs = array(
				'lg' => 'an',
			);
			
			// Apply specific manufacturer rules
			if (key_exists(strtolower($make), $mfgs)) {
				$friendly = $mfgs[strtolower($make)].' '.$friendly;
			}
			// Apply regular english rules regarding a/an
			else {
				$friendly = (strspn(strtolower($friendly), "aeiou") == 1) ? 'an '.$friendly : 'a '.$friendly;
			}
			
		}
		
		// Return the friendly string
		return $friendly;
		
	}
	
	/**
	 * Get a formatted list of the users that have favorited this photo.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return string The list of users who have favorited the photo
	 */
	public function get_favorited_list() {
		$favorited = '<ul>';
		
		// Show only the last 5 people who favorited (otherwise the list is too long)
		$this->favorite['person'] = array_slice($this->favorite['person'], 0, 5);
		foreach ($this->favorite['person'] as $person) {
			$favorited .= '<li><img src="http://flickr.com/buddyicons/'.$person['nsid'].'.jpg" /><div class="user"><b><a href="'.$this->flickr->urls_getUserPhotos($person['nsid']).'">'.$person['username'].'</a></b> added this as a favorite on <b>'.date('j M y', $person['favedate']).'</b>.</div><div class="other_favorites">&raquo; See <a href="'.$this->flickr->urls_getUserPhotos($person['nsid']).'favorites/">'.$person['username'].'\'s <b>other favorites</b> here</a>.</div><div class="clearfix"></div></li>';
		}
		
		// If we cut it off, give a link to the full list.
		if ($this->favorite['total'] == 6) {
			$favorited .= '<li><a href="'.$this->flickr->urls_getUserPhotos($this->nsid).$this->photo['id'].'/favorites/">And 1 other.</a></li>';
		}
		else if ($this->favorite['total'] > 6) {
			$favorited .= '<li><a href="'.$this->flickr->urls_getUserPhotos($person['nsid']).$this->photo['id'].'/favorites/">And '.($this->favorite['total'] - 5).' others.</a></li>';
		}
		
		$favorited .= '</ul>';
		
		return $favorited;
	}
	
	/**
	 * Gets a friendly location string for the photo.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param array $geo The array returned from flickr.photos.geo.getLocation
	 * @param array $geo_perms The geo permissions array from flickr.photos.geo.getPerms
	 * @return string The friendly string
	 */
	public static function get_geo_string($geo) {
		// Replace spaces with + for cleaner urls
		$country = $geo['country'];
		$region = $geo['region'];
		$county = $geo['county'];
		$locality = $geo['locality'];
		$neighborhood = $geo['neighbourhood'];
		
		// Create the links
		$country_link = '<a href="http://flickr.com/places/'.$country['place_id'].'">'.$country['_content'].'</a>';
		$region_link = '<a href="http://flickr.com/places/'.$region['place_id'].'">'.$region['_content'].'</a>';
		$county_link = '<a href="http://flickr.com/places/'.$county['place_id'].'">'.$county['_content'].' County</a>';
		$locality_link = '<a href="http://flickr.com/places/'.$locality['place_id'].'">'.$locality['_content'].'</a>';
		$neighborhood_link = '<a href="http://flickr.com/places/'.$neighborhood['place_id'].'">'.$neighborhood['_content'].'</a>';
		
		/*
		Neighborhood: Will return Neigborhood, Locality, Region
		*/
		if (isset($geo['neighbourhood'])) {
			return $neighborhood_link.', '.$locality_link.', '.$region_link;
		}
		/*
		Locality: Will return Locality, Region
		*/
		if (isset($geo['locality'])) {
			return $locality_link.', '.$region_link;
		}
		/*
		County: Will return County, Region
		*/
		if (isset($geo['county'])) {
			return $county_link.', '.$region_link;
		}
		/*
		Region: Will return Region, Country
		*/
		if (isset($geo['region'])) {
			return $region_link.', '.$country_link;
		}
		/*
		Country only
		*/
		return $country_link;
	}
	
	/**
	 * Returns a friendly string for the date of the photo.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param array $photo The array returned from photos_getInfo
	 * @return string The friendly list of tags
	 */
	public function get_photo_date($photo) {
		$granularity = $photo['dates']['takengranularity'];
		$date_taken = strtotime($photo['dates']['taken']);
		$date_posted = $photo['dates']['posted'];
		
		// Date taken
		$append = true;
		if ($granularity == 6) {
			$date = 'Taken some time in <a href="'.$this->flickr->urls_getUserPhotos($this->nsid).'archives/undated/'.date("Y/", $date_taken).'">'.date("Y", $date_taken).'</a>';
		}
		else if ($granularity == 4) {
			$date = 'Taken in <a href="'.$this->flickr->urls_getUserPhotos($this->nsid).'archives/undated/'.date("Y/m/", $date_taken).'">'.date("F Y", $date_taken).'</a>';
		}
		else {
			
			if (date("F j, Y", $date_taken) == date("F j, Y", $date_posted)) {
				$date = 'Taken and posted to Flickr on <a href="'.$this->flickr->urls_getUserPhotos($this->nsid).'archives/date-taken/'.date("Y/m/d/", $date_taken).'">'.date("F j, Y", $date_taken).'</a>';
				$append = false;
			}
			else {
				$date = 'Taken on <a href="'.$this->flickr->urls_getUserPhotos($this->nsid).'archives/date-taken/'.date("Y/m/d/", $date_taken).'">'.date("F j, Y", $date_taken).'</a>';
			}
			
		}
		
		// Append the posted date
		if ($append) {
			$date .= ' and posted to Flickr on <a href="'.$this->flickr->urls_getUserPhotos($this->nsid).'archives/date-posted/'.date("Y/m/d/", $date_posted).'">'.date("F j, Y", $date_posted).'</a>';
		}
		
		return $date;
	}
	
	/**
	 * Returns an array with the dimensions of the photo for the specified size
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $the_size The size description
	 * @return array An array with the width and height
	 */
	public function get_photo_size($the_size = 'Medium') {
		foreach ($this->sizes as $size) {
			if ($size['label'] == $the_size) {
				return array(
					'width' => $size['width'],
					'height' => $size['height']
				);
			}
		}
	}
	
	/**
	 * Returns a friendly string with the tags from a photo. The string
	 * is comma-delimited and last two tags are separated with 'and'.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param array $photo The array returned from photos_getInfo
	 * @return string The friendly list of tags
	 */
	public function get_tag_string($photo) {
		
		// Get the number of tags
		$tag_count = count($photo['tags']['tag']);
		
		// Create the blank string
		$tags = '';
		
		// If there are tags
		if ($tag_count > 0) {
										
			// Create an array of the tags with links excluding machine tags
			$tag_array = array();
			for ($i = 0; $i < $tag_count; $i++) {
				if (!$photo['tags']['tag'][$i]['machine_tag']) {
					$raw = $photo['tags']['tag'][$i]['raw'];
					$content = $photo['tags']['tag'][$i]['_content'];									
					$s = '<a href="'.$this->flickr->urls_getUserPhotos($this->nsid).'tags/'.urlencode($content).'/">'.$raw.'</a>';
					array_push($tag_array, $s);
				}
			}
										
			// Output the tags in a friendly string
			$tags = Utilities::implodeProper($tag_array);
			
		}
		
		// Return the string
		return $tags;
		
	}
	
	/**
	 * Gets the UIDs of users tagged in the Flickr photo.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $pid The id of the flickr photo
	 * @return array An array of the tagged uids
	 */
	public static function get_tagged_users($pid, $token) {
		
		// Create a temporary Flickr object so we don't use the cache
		$temp_flickr = new phpFlickr(FLICKR_API_KEY, FLICKR_API_SECRET);
		$temp_flickr->setToken($token);
		$tags = $temp_flickr->tags_getListPhoto($pid);
		$temp_flickr = null;
		
		// Create the array of the tagged users
		$fb_users = array();
		$fb_tags = array();
		if (count($tags) > 0) {
			foreach ($tags as $tag) {
				if ($tag['machine_tag']) {
					
					// User tag
					if (strpos($tag['raw'], 'fb:user') !== false) {
						$user = substr($tag['raw'], 8);
						$fb_users[] = array(
							'tag_content' => $user,
							'tag_id' => $tag['id'],
							'uid' => $user
						);
					}
					else if (strpos($tag['raw'], 'fb:tag') !== false) {
						$tag_info = explode(',', substr($tag['raw'], 7));
						$fb_tags[] = array(
							'tag_content' => $tag_info[0],
							'tag_x' => $tag_info[1],
							'tag_y' => $tag_info[2],
							'tag_id' => $tag['id']
						);
					}
					
				}
			}
		}
		
		// Combine arrays
		$tagged_users = array();
		foreach ($fb_tags as $tag) {
			
			$user_tag = Utilities::multiArraySearch($tag['tag_content'], $fb_users);
			if ($user_tag !== false) {
				$index = $user_tag[0];
				$tagged_users[] = array(
					'complete' => 1,
					'tag_content' => $tag['tag_content'],
					'tag_x' => $tag['tag_x'],
					'tag_y' => $tag['tag_y'],
					'tag_ids' => array($tag['tag_id'], $fb_users[$index]['tag_id'])
				);
				array_splice($fb_users, $index, 1);
			}
			else {
				$tagged_users[] = array(
					'complete' => 1,
					'tag_content' => $tag['tag_content'],
					'tag_x' => $tag['tag_x'],
					'tag_y' => $tag['tag_y'],
					'tag_ids' => array($tag['tag_id'])
				);
			}
			
		}
		
		// Add the rest of the users with no location
		foreach ($fb_users as $tag) {
			$tagged_users[] = array(
				'complete' => 0,
				'tag_content' => $tag['tag_content'],
				'tag_ids' => array($tag['tag_id'])
			);
		}
		
		return $tagged_users;
		
	}
	
	/**
	 * Generates the string for the tagged users in a photo.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $pid The id of the flickr photo
	 * @param array $tag_note_assoc The array from the get_associated_notes function
	 * @return string The string of users tagged in the photo or False if no one is tagged
	 */
	public static function get_tagged_users_string($owner_uid, $pid, $tagged_users) {
				
		if (count($tagged_users) > 0) {
			$users = array();
			foreach ($tagged_users as $tag) {
				
				$token = hash(FlickrPhotosets::hash_method, $owner_uid.$pid.FACEBOOK_API_SECRET.implode(',', $tag['tag_ids']));
				
				$string = '';
				if ($tag['complete']) {
					$string .= '<span onmouseover="showTagAt(\''.$tag['tag_x'].'\', \''.$tag['tag_y'].'\');" onmouseout="hideTag();">';
				}
				else {
					$string .= '<span>';
				}
				if (is_numeric($tag['tag_content'])){
					$string .= '<fb:name uid="'.$tag['tag_content'].'" useyou="false" />';
					if (!$tag['complete']) {
						$string .= '<fb:if-is-user uid="'.$owner_uid.'"><img src="{callbackURL}/images/exclamation.png" height="12" width="12" alt="We couldn\'t find this person. Please re-tag them." title="We couldn\'t find this person in the photo. Please re-tag them." /></fb:if-is-user>';
					}
					$string .= ' (<a href="{canvasURL}/photos/of/'.$tag['tag_content'].'/">photos</a>';
					$string .= '<fb:if-is-user uid="'.$owner_uid.','.$tag['tag_content'].'">, ';
					$string .= '<a onclick="removeTags(\''.implode(',', $tag['tag_ids']).'\', \''.$token.'\')">remove</a>';
					$string .= '</fb:if-is-user>)';
				}
				else {
					$string .= $tag['tag_content'];
					$string .= '<fb:if-is-user uid="'.$owner_uid.','.$tag['tag_content'].'"> (';
					$string .= '<a onclick="removeTags(\''.implode(',', $tag['tag_ids']).'\', \''.$token.'\')">remove</a>';
					$string .= '</fb:if-is-user>)';
				}
				$string .= '</span>';
				
				$users[] = $string;
				
				
				/*
				'complete' => t/f
				'tag_content' => $user_tag['tag_content'],
				'tag_x' => $fb_tags[$index]['tag_x'],
				'tag_y' => $fb_tags[$index]['tag_y'],
				'tag_ids' => array($user_tag['tag_id'], $fb_tags[$index]['tag_id'])
				*/
				
			}
			if (count($users) > 0) {
				return 'In this photo: '.Utilities::implodeProper($users).'.';
			}
		}
		return false;
		
	}
	
	/**
	 * Prepares the information for a photo in a photoset.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function in_photoset() {
		$this->tab = 'photosets';
		
		// Get all of the photos in the set
		$this->photos = $this->flickr->photosets_getPhotos($_GET['sid']);

		// Remove photos that do not match the privacy settings
		$this->photos['photo'] = $this->remove_photos($this->photos['photo']);

		// Get the index and count
		$current_photo_index = 0;
		foreach ($this->photos['photo'] as $photo_index) {
			if ($photo_index['id'] == $this->photo['id']) {
				break;
			}
			$current_photo_index++;
		}
		$this->current_photo = $current_photo_index + 1;
		$this->photo_count = count($this->photos['photo']);

		// Get photoset information
		$this->photoset = $this->flickr->photosets_getInfo($_GET['sid']);
		$this->title = $this->photoset['title'];

		// Get the context for the photo
		$this->photos['photo'] = array_values($this->photos['photo']);
		$this->context['nextphoto']['id'] = $this->photos['photo'][$current_photo_index+1]['id'];
		$this->context['prevphoto']['id'] = $this->photos['photo'][$current_photo_index-1]['id'];
		$this->url_append = '/in/set-'.$this->photoset['id'];
	}
	
	/**
	 * Prepares the information for a photo in the photostream.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function in_photostream() {
		$this->tab = 'photostream';
		
		// This is in the photostream - INCOMPLETE
		// Need to make this section work with privacy settings
		
		// Redirect to photo in set, if possible, if the photostream is disabled
		if (!$this->config['enable_gallery_photostream']) {

			//$all_contexts = $this->flickr->photos_getAllContexts($this->photo['id']);

			// If there is a set, redirect
			if (isset($all_contexts['set'][0])) {
				$this->facebook->redirect($this->canvas_url.'/photos/'.$this->uid.'/'.$this->photo['id'].'/in/set-'.$all_contexts['set'][0]['id'].'/');
			}
			// Otherwise, show error message
			else {
				$this->error(FlickrPhotosetsErrors::$photo_not_in_set);
			}

		}

		// Get the index and count
		$this->photos = $this->flickr->people_getPublicPhotos($this->nsid, '', 500);
		$this->photo_count = $this->photos['total'];

		$photos_all = $this->photos['photo'];
		for ($i = 1; $i < $this->photos['pages']; $i++) {
			$photos_page = $this->flickr->people_getPublicPhotos($this->nsid, '', 500, $i+1);
			$photos_all = array_merge($photos_all, $photos_page['photo']);
		}

		//$photos_all = array_reverse($photos_all);
		$current_photo_index = 0;
		foreach ($photos_all as $photo_index) {
			if ($photo_index['id'] == $this->photo['id']) {
				break;
			}
			$current_photo_index++;
		}
		$this->current_photo = $current_photo_index + 1;

		// Reset the photos array for use
		$this->photos['photo'] = $photos_all;

		// Get the context of the photo
		$this->context = $this->flickr->photos_getContext($this->photo['id']);
		
		// Reverse the contexts so the navigation works like most people expect
		$context_next = $this->context['nextphoto'];
		$context_prev = $this->context['prevphoto'];
		$this->context['nextphoto'] = $context_prev;
		$this->context['prevphoto'] = $context_next;

		// Get photostream information
		$this->title = 'Photostream';
	}
	
	/**
	 * Prepares the information for a photo of a tagged user.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function of_user() {
		$this->tab = 'tagged';
		
		// Get the photos
		$this->of = $_GET['of'];
		$this->photos = $this->get_tagged_photos_of($this->of);
		$this->photo_count = $this->photos['total'];

		// Get the index
		$current_photo_index = 0;
		foreach ($this->photos['photo'] as $photo_index) {
			if ($photo_index['id'] == $this->photo['id']) {
				break;
			}
			$current_photo_index++;
		}
		$this->current_photo = $current_photo_index + 1;

		// Get the context
		$this->context['nextphoto']['id'] = $this->photos['photo'][$current_photo_index+1]['id'];
		$this->context['prevphoto']['id'] = $this->photos['photo'][$current_photo_index-1]['id'];
		$this->url_append = '/of/'.$this->of;

		// Title
		$this->title = 'Flickr photos of <fb:name uid="'.$this->of.'" capitalize="true" />';
		
	}
	
}

?>